from emmodels.em_basemodel import BaseModel
from emmysql.em_sqlmanager import engine, metaData
from sqlalchemy.schema import Table
from sqlalchemy.sql import select
from sqlalchemy import desc
import datetime
from emmongo import em_mongomanager as mongoManager
from LSTM.variableset import EMVar

class HisSeries(BaseModel):
    _db_mapper = {
        'stockid':'code',
        'datatime': 'date',
        'period': 'period',
        'high': 'high',
        'low': 'low',
        'open': 'open',
        'close': 'close',
        'volume': 'vloume',
        'amount': 'amount',
        'createtime': 'createtime',
        # 'MACD': 'MACD',
        # 'KDJ': 'KDJ',
        # 'MA5': 'MA5',
        # 'is_valid': ''
    }
    _table = Table('EM_HIS_SERIES', metaData, autoload=True)
    BaseModel.loadTable('EM_HIS_SERIES')

    def __init__(self,stockid=None, datatime=None, period=None, high=None,low=None,open=None,close=None,
                 volume=None,amount=None,MACD=None,KDJ=None,MA5=None,is_valid=None,**kwargs):
        self.stockid = stockid
        self.datatime = datatime
        self.period = period
        self.high = high
        self.low = low
        self.open = open
        self.close= close
        self.volume = volume
        self.amount = amount
        self.MACD = MACD
        self.KDJ = KDJ
        self.MA5 = MA5
        self.createtime = datetime.datetime.now()
        self.is_valid = is_valid

    @classmethod
    def select_last(cls,stockid):
        connect = engine.connect()
        rp = connect.execute(select([cls._table]).where(cls._table.c.stockid == stockid).order_by(desc(cls._table.c.datatime)))
        return rp.first()


class ModuleHisSeries(HisSeries):
    _table = Table('EM_MODULE_HIS_SERIES', metaData, autoload=True)
    BaseModel.loadTable('EM_MODULE_HIS_SERIES')


class DetailHisSeries(HisSeries):
    _mCOL = mongoManager.db[mongoManager.COL_DETAIL_DAY_HIS_DATA]
    # kwarts 用来存放所有的指标
    def __init__(self,stockid=None, datatime=None, period=None, high=None,low=-1,open=-1,close=-1,
                 volume=-1,amount=-1,is_index=False, fqType='qfq',is_valid=None,**kwargs):
        self.code = stockid
        self.datatime = datatime
        self.period = period
        self.high = high
        self.low = low
        self.open = open
        self.close= close
        self.volume = volume
        self.amount = amount
        self.updateTime = datetime.datetime.now()
        self.is_index = is_index
        self.fqType = fqType
        self.is_valid = is_valid
        for name in kwargs.keys():
            setattr(self, name, kwargs.pop(name))

    def insert_one(self):
        mongoManager.db[mongoManager.COL_DETAIL_DAY_HIS_DATA].update_one({EMVar.code:self.stockid, EMVar.datetime:self.datetime,
                                                                                                 'is_index':self.is_index, EMVar.fqType:self.fqType}, {'$set':self.__dict__},
                                                                         upsert=True)
    def select_last(cls,stockid,is_index=False,fqType='qfq'):
        mongoManager.db[mongoManager.COL_DETAIL_DAY_HIS_DATA].find_one({'$query':{EMVar.code:stockid, 'is_index':is_index, EMVar.fqType:fqType}, '$orderby':{EMVar.datetime:-1}})




